package entity

import (
	"fmt"

	"github.com/jinzhu/gorm"
	"shqsoft.cn/pasnj/base"
)

//COMP_CLIE_JC 用户
type COMP_CLIE_JC struct {
	ID          int     `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	CUID        int     `gorm:"column:CUID"`
	CUID_GL1    int     `gorm:"column:CUID_GL1"`
	CUID_GL2    int     `gorm:"column:CUID_GL2"`
	CUID_GL3    int     `gorm:"column:CUID_GL3"`
	EMID1       int     `gorm:"column:EMID1"`
	EMID2       int     `gorm:"column:EMID2"`
	KHMC        string  `gorm:"column:KHMC"`
	GLKH1       string  `gorm:"column:GLKH1"`
	GLKH2       string  `gorm:"column:GLKH2"`
	GLKH3       string  `gorm:"column:GLKH3"`
	ZZRR        string  `gorm:"column:ZZRR"`
	FZRR        string  `gorm:"column:FZRR"`
	ZZRRXM      string  `gorm:"column:ZZRRXM"`
	ZZRRCaption string  `gorm:"-"`
	FZRRXM      string  `gorm:"column:FZRRXM"`
	FZRRCaption string  `gorm:"-"`
	SYLX        string  `gorm:"column:SYLX"`
	SYLXCaption string  `gorm:"-"`
	SJCR        string  `gorm:"column:SJCR"`
	EJCR        string  `gorm:"column:EJCR"`
	LSBZ        float32 `gorm:"column:LSBZ"`
	BZ          string  `gorm:"column:BZ"`
}

//TableName User用户表名
func (COMP_CLIE_JC) TableName() string {
	return "T_COMP_CLIE_JC"
}

func (o *COMP_CLIE_JC) BeforeCreate(tx *gorm.DB) (err error) {
	sqlEx := "SELECT DISTINCT ID CUID FROM T_COMP_CLIE WHERE CUNM=?"
	row := tx.Raw(sqlEx, o.KHMC).Row()
	row.Scan(&o.CUID)
	if o.CUID == 0 && o.KHMC != "内部户" {
		return fmt.Errorf("客户:【%s】在系统中不存在！", o.KHMC)
	}
	// if o.GLKH1 != "" {
	// 	sqlEx = "SELECT DISTINCT CUID FROM V_COMP_ACCO_FLOW WHERE CUNM=?"
	// 	row = tx.Raw(sqlEx, o.GLKH1).Row()
	// 	row.Scan(&o.CUID_GL1)
	// 	if o.CUID_GL1 == 0 {
	// 		return fmt.Errorf("关联客户1:【%s】在系统中不存在！", o.GLKH1)
	// 	}
	// }
	// if o.GLKH2 != "" {
	// 	sqlEx = "SELECT DISTINCT CUID FROM V_COMP_ACCO_FLOW WHERE CUNM=?"
	// 	row = tx.Raw(sqlEx, o.GLKH2).Row()
	// 	row.Scan(&o.CUID_GL2)
	// 	if o.CUID_GL2 == 0 {
	// 		return fmt.Errorf("关联客户2:【%s】在系统中不存在！", o.GLKH2)
	// 	}
	// }
	// if o.GLKH3 != "" {
	// 	sqlEx = "SELECT DISTINCT CUID FROM V_COMP_ACCO_FLOW WHERE CUNM=?"
	// 	row = tx.Raw(sqlEx, o.GLKH3).Row()
	// 	row.Scan(&o.CUID_GL3)
	// 	if o.CUID_GL3 == 0 {
	// 		return fmt.Errorf("关联客户3:【%s】在系统中不存在！", o.GLKH3)
	// 	}
	// }
	sqlEx = "select ID,SysUserName from T_SysUser where SysUserID=?"
	row = tx.Raw(sqlEx, o.ZZRR).Row()
	row.Scan(&o.EMID1, &o.ZZRRXM)
	if o.FZRR != "" {
		row = tx.Raw(sqlEx, o.FZRR).Row()
		row.Scan(&o.EMID2, &o.FZRRXM)
	}
	var count int
	sqlEx = "select COUNT(*) count from T_COMP_CLIE_JC where KHMC=? and SYLX=? and SJCR=? and EJCR=?"
	row = tx.Raw(sqlEx, o.KHMC, o.SYLX, o.SJCR, o.EJCR).Row()
	row.Scan(&count)
	if count > 0 {
		return fmt.Errorf("客户:【%s】已存在监测指标，请勿重复录入！", o.KHMC)
	}
	return
}
func (c *COMP_CLIE_JC) AfterFind() (err error) {
	if c.SYLX == "1" {
		c.SYLXCaption = "存款日均"
	} else if c.SYLX == "2" {
		c.SYLXCaption = "存款利润"
	} else if c.SYLX == "3" {
		c.SYLXCaption = "综合收益"
	} else if c.SYLX == "4" {
		c.SYLXCaption = "综合收益率"
	} else if c.SYLX == "5" {
		c.SYLXCaption = "用信率"
	}
	c.ZZRRCaption = c.ZZRRXM + "(" + c.ZZRR + ")"
	if c.FZRRXM != "" {
		c.FZRRCaption = c.FZRRXM + "(" + c.FZRR + ")"
	}
	return
}

// COMP_CLIE_JCEF G
func COMP_CLIE_JCEF() base.EntityFunc {
	return func() interface{} {
		entity := COMP_CLIE_JC{}
		return &entity
	}
}

//COMP_CLIE_JCESF GJ
func COMP_CLIE_JCESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []COMP_CLIE_JC
		return &entities
	}
}
func init() {
	base.DaoPoolObj.Put("COMP_CLIE_JC", base.NewDefaultDao(COMP_CLIE_JCEF(), COMP_CLIE_JCESF(), nil))
}
